package com.gary.controller;

import com.gary.entity.User;
import com.gary.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Optional;

/**
 * @author pengrui
 * @date 2020/11/26 - 9:27
 */
@RestController
public class UserController {

    @Autowired
    UserRepository userRepository;

    @GetMapping("/user/{id}")
    public User findOne(@PathVariable("id") Integer id) {
        Optional<User> byId = userRepository.findById(id);
        User user = byId.get();
        System.out.println(user);
        return user;
    }

    @GetMapping("/insertUser")
    public User insertOne(User user) {
        //User save = userRepository.save(user);
        return user;
    }

    @GetMapping("/all")
    public List<User> findAll() {
        List<User> all = userRepository.findAll();
        return all;
    }
}
